package com.googlecode.droiddip;

/* 
 * 
 * View that draws the diplomacy map
 * The map is made up of a Vector of Provinces
 * 
 */

import com.googlecode.droiddip.Province;
import com.googlecode.droiddip.Border;
import android.graphics.Point;

//For View
import android.view.View;
import android.content.Context;
//For graphics
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.Path;
import android.graphics.Color;
//other
//~ import android.widget.Toast;
import java.util.Vector;

public class MapView extends View
{
	
	private Paint textPaint = new Paint();
	private Paint landPaint = new Paint();
	private Paint seaPaint = new Paint();
	private Paint neutralPaint = new Paint();
	private Paint borderPaint = new Paint();
	private Paint borderPaint2 = new Paint();
	private Vector<Province> provinces = new Vector();	
	private Path borders = new Path();
	
	public MapView (Context context)
	{
		super(context);
		this.setBackgroundColor(	Color.WHITE);
		
		//set up paintbox
		textPaint.setColor(Color.BLACK);
		textPaint.setTextSize(10);
		textPaint.setFakeBoldText(true); 		
		textPaint.setAntiAlias(true);
		landPaint.setColor(Color.RED);
		seaPaint.setColor(Color.CYAN);
		neutralPaint.setColor(Color.YELLOW);
		borderPaint.setColor(Color.BLACK);
		borderPaint.setStyle(Paint.Style.STROKE);
		borderPaint.setAntiAlias(true);
		borderPaint2.setColor(Color.WHITE);
		borderPaint2.setAlpha(150);
		borderPaint2.setStyle(Paint.Style.STROKE);
		borderPaint2.setStrokeWidth(10);
		borderPaint2.setAntiAlias(true);

	}
	
	@Override
	protected void onDraw(Canvas canvas)
	{

		for (Province province : provinces)
		{
			if (province.type == Province.Type.SEA) 
				canvas.drawPath(province, seaPaint);
			else if (province.type == Province.Type.NEUTRAL)
				canvas.drawPath(province, neutralPaint);
			else
				canvas.drawPath(province, landPaint);
			
		}
		
		canvas.drawPath(borders, borderPaint2);
		canvas.drawPath(borders, borderPaint);
				
		for (Province province : provinces)
			if (province.type != Province.Type.NEUTRAL)
				canvas.drawText(province.abbreviation, province.label.x, province.label.y, textPaint);
				//~ canvas.drawText(province.abbreviation, province.centre.x, province.centre.y, textPaint);
	}
	
	public Province addProvince(String name, String abbreviation, Province.Type provinceType,int labelX, int labelY, Border... borders)
	{
		Province newProvince = new Province(name, abbreviation, provinceType, new Point (labelX, labelY), borders );
		provinces.add(newProvince);	
		return newProvince;
	}
	
	public Border addBorder(String borderText)
	{
		Border border = Border.createFromString(borderText);
		Path path = (Path)border;
		borders.addPath(path);
		return border;
	}
	
	public static MapView BritainTest(Context context)
	{
		//Create a view populated with some hard-coded provinces for testing. In future this should be done with an XML file.
		MapView britain = new MapView(context);
		
		Border borderClyEdi = britain.addBorder("144,213 146,200 152,194");
		Border borderClyLyp = britain.addBorder("138,214 144,213");
		Border borderClyNAt = britain.addBorder("138,214 130,208 129,197 139,189 140,182 148,177");
		Border borderClyNrg = britain.addBorder("152,194 154,188 161,185 162,181 148,177");
		Border borderEdgNAt = britain.addBorder("148,0 0,0 0,273");
		Border borderEdgNrg = britain.addBorder("148,0 362,0");
		Border borderEdiLvp = britain.addBorder("144,213 145,217 155,228");
		Border borderEdiNrg = britain.addBorder("152,194 158,193 171,197");
		Border borderEdiNth = britain.addBorder("171,197 170,202 165,210 158,214 151,215 157,216 161,218 163,226");
		Border borderEdiYor = britain.addBorder("155,228 163,226");
		Border borderEngIri = britain.addBorder("88,303 100,291");
		Border borderEngLon = britain.addBorder("168,296 160,298 147,295");
		Border borderEngNth = britain.addBorder("168,296 173,301");
		Border borderEngWal = britain.addBorder("100,291 110,292 120,295 124,291 134,294 147,295");
		Border borderIrdIri = britain.addBorder("120,227 110,232 109,246 98,259 87,257 70,261");
		Border borderIrdNAt = britain.addBorder("70,261 64,250 67,242 71,245 81,234 74,228 80,225 78,218 82,217 89,220 94,220 95,218 94,216 97,216 101,212 110,212 119,217 120,227");
		Border borderIriLvp = britain.addBorder("128,262 126,256 121,257 132,250 135,250 139,240 136,229 130,227");
		Border borderIriMid = britain.addBorder("58,273 88,303");
		Border borderIriNat1 = britain.addBorder("70,261 58,273");
		Border borderIriNat2 = britain.addBorder("130,227 120,227");
		Border borderIriWal = britain.addBorder("100,291 112,287 122,281 130,282 127,276 119,272 116,272 115,265 128,262");
		Border borderLonNth = britain.addBorder("166,269 168,270 171,268 177,270 178,274 176,283 165,293 172,294 168,296");
		Border borderLonWal = britain.addBorder("147,295 145,281 150,277 153,271");
		Border borderLonYor = britain.addBorder("166,269 153,271");
		Border borderLvpNAt = britain.addBorder("130,227 130,223 138,217 138,214");
		Border borderLypWal = britain.addBorder("128,262 143,262 150,264");
		Border borderLypYor = britain.addBorder("150,264 151,248 155,239 155,228");
		Border borderMidNAt = britain.addBorder("0,273 58,273");
		Border borderNAtNrg = britain.addBorder("148,177 148,0");
		Border borderNrgNth = britain.addBorder("236,154 171,181 171,197");
		Border borderNthYor = britain.addBorder("163,226 163,239 168,246 170,252 169,265 166,269");
		Border borderWalYor = britain.addBorder("153,271 150,264");
		
		britain.addProvince("North Sea","Nth",Province.Type.SEA,190,230,borderNrgNth,borderEdiNth,borderNthYor,borderLonNth,borderEngNth);
		britain.addProvince("English Channel","Eng",Province.Type.SEA,135,306,borderEngNth,borderEngLon,borderEngWal,borderEngIri);
		britain.addProvince("Edinburgh","Edi",Province.Type.LAND,152,202,borderClyEdi, borderEdiNrg, borderEdiNth, borderEdiYor, borderEdiLvp);		
		britain.addProvince("Clyde","Cly",Province.Type.LAND,133,201,borderClyEdi,borderClyLyp,borderClyNAt,borderClyNrg);
		britain.addProvince("Liverpool","Lvp",Province.Type.LAND,138,230,borderClyLyp,borderEdiLvp,borderLypYor,borderLypWal,borderIriLvp,borderLvpNAt);
		britain.addProvince("Wales","Wal",Province.Type.LAND,130,275,borderIriWal,borderLypWal,borderWalYor,borderLonWal,borderEngWal);
		britain.addProvince("Yorkshire","Yor",Province.Type.LAND,155,254,borderWalYor,borderLypYor,borderEdiYor,borderNthYor,borderLonYor);
		britain.addProvince("London","Lon",Province.Type.LAND,160,280,borderLonYor, borderLonNth, borderEngLon, borderLonWal );
		britain.addProvince("Norwegian Sea","Nrg",Province.Type.SEA,220,70,borderEdgNrg,borderNAtNrg,borderClyNrg,borderEdiNrg,borderNrgNth);
		britain.addProvince("Irish Sea","Iri",Province.Type.SEA,95,270,borderIriNat1,borderIrdIri,borderIriNat2,borderIriLvp,borderIriWal,borderEngIri);
		britain.addProvince("North Atlantic Ocean","Nat",Province.Type.SEA,65,120,borderEdgNAt,borderNAtNrg,borderClyNAt,borderLvpNAt,borderIriNat2,borderIrdNAt,borderIriNat1);
		britain.addProvince("Ireland"," ",Province.Type.NEUTRAL,0, 0,borderIrdIri,borderIrdNAt);
		
		return britain;		
	}
	
	private static Path pathFromString(String points)
	{
		String[] points1 = points.split(" ");
		String[] points2 = points1[0].split(",");
		Path path = new Path();
		path.moveTo(Integer.parseInt(points2[0]),Integer.parseInt(points2[1]));
		for (int i=1; i < points1.length; ++i) {
			points2 = points1[i].split(",");
			path.lineTo(Integer.parseInt(points2[0]),Integer.parseInt(points2[1]));
		}
		//~ path.close();
		return path;
	}
}
